/*
 * $Id: DomainModelTypeNamingConvention.java 301 2006-08-03 10:25:18Z petterei $
 */
package com.wgo.bpot.common.util;

import com.wgo.bpot.common.transport.exception.db.DatabaseException;

/**
 * Used to arrange for intelligent reflection.
 * 
 * {@url http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.8.2}
 * 
 * @author petterei
 * @version $Rev: 301 $
 */
public final class DomainModelTypeNamingConvention {
	
	
	@SuppressWarnings("unchecked")
	public static <T extends Object> Class<T> loadImplClass(Class<T> domainModelInterface){
		String implClassName = domainModelInterface.getPackage().getName() + ".impl." + domainModelInterface.getSimpleName() + "Impl";
		Class<T> implClass = null;
		try {
			implClass = (Class<T>)Class.forName(implClassName);
		} catch (ClassNotFoundException e) {
			throw new DatabaseException("Class not found: " + implClassName, e);
		}
		return implClass;
	}
	
	@SuppressWarnings("unchecked")
	public static <T extends Object> Class<T> loadServerClass(Class<T> domainModelInterface)
	{
		String path = domainModelInterface.getPackage().getName();
		int index = path.lastIndexOf("common");
		String subpath = path.substring(0,index-1);
		String implClassName = subpath  + ".server.ejb." + domainModelInterface.getSimpleName() + "Ejb";
		Class<T> implClass = null;
		try {
			implClass = (Class<T>)Class.forName(implClassName);
		} catch (ClassNotFoundException e) {
			throw new DatabaseException("Class not found: " + implClassName + ".  domainClass=" + domainModelInterface.getName(), e);
		}
		return implClass;
	}
	
	private DomainModelTypeNamingConvention () {} // This class is not ment to be instantiatated
	
	
	public static Class getApiClassFromEjbClass(Class ejbClass) {
		String domainPackageName = ejbClass.getName().substring(0,ejbClass.getName().lastIndexOf(".ejb."));
		String apiClassName = domainPackageName + "." + ejbClass.getSimpleName().substring(0,ejbClass.getSimpleName().indexOf("Ejb"));
		Class result = null;
		try {
			result = Class.forName(apiClassName,true,Thread.currentThread().getContextClassLoader());
		} catch (ClassNotFoundException e) {
			throw new DatabaseException("Class not found: " + apiClassName, e);
		}
		return result;
	}

	public static Class getImplClassFromEjbClass(Class ejbClass) {
		String domainPackageName = ejbClass.getName().substring(0,ejbClass.getName().lastIndexOf(".server.ejb."));
		String apiClassName = domainPackageName + ".common.impl." + ejbClass.getSimpleName().substring(0,ejbClass.getSimpleName().lastIndexOf("Ejb")) + "Impl";
		Class result = null;
		try {
			result = Class.forName(apiClassName,true,Thread.currentThread().getContextClassLoader());
		} catch (ClassNotFoundException e) {
			throw new DatabaseException("Class not found: " + apiClassName, e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public static <T> Class<T> loadClass(String className) throws ClassNotFoundException {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();

		if (loader != null) {
			return (Class<T>) Class.forName(className, false, loader);
		}
		return (Class<T>)Class.forName(className);
	}

}

